home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9005.ZIP / AYERS.ZIP / CHCKTCNT.CLS < prev    next >
Text File  |  1990-02-26  |  4KB  |  146 lines

  1.  
  2. MarketActor subclass: #CheckoutCounter
  3.   instanceVariableNames: 
  4.     'checker bagger customers nowServing '
  5.   classVariableNames: 
  6.     'MaxCustomers '
  7.   poolDictionaries: '' !
  8.  
  9. !CheckoutCounter class methods !
  10.  
  11. imageName
  12.     ^'Counter'.!
  13.  
  14. initialize
  15.     MaxCustomers isNil ifTrue:[MaxCustomers := 3].!
  16.  
  17. maxCustomers
  18.     self initialize.
  19.     ^MaxCustomers.!
  20.  
  21. maxCustomers:aNumber
  22.     MaxCustomers := aNumber.!
  23.  
  24. new
  25.     self initialize.
  26.     ^super new.!
  27.  
  28. priority
  29.     ^3.! !
  30.  
  31.  
  32. !CheckoutCounter methods !
  33.  
  34. addCustomer
  35.     |aCustomer|
  36.     aCustomer := self receive.
  37.     running
  38.         ifTrue:[
  39.             customers add:aCustomer.
  40.             self length == 1
  41.                 ifTrue:[self nextCustomer]].!
  42.  
  43. checkoutPosition
  44.         "Answer a Point, the checkout position."
  45.     ^position - (Customer width @ 0).!
  46.  
  47. display
  48.     super display.
  49.     checker display.
  50.     bagger display.!
  51.  
  52. endOfLinePosition
  53.         "Answer a Point, the end of the checkout line."
  54.     ^position
  55.         - (Customer width
  56.             @ (customers size + 1 * Customer height)).!
  57.  
  58. exitPosition
  59.         "Answer a Point, the exit position."
  60.     ^position + ((0 - Customer width) @ self height).!
  61.  
  62. initialize
  63.     checker := Checker new.
  64.     bagger  := Bagger new.
  65.     checker bagger:bagger.
  66.     bagger  checker:checker.
  67.     customers := OrderedCollection new:MaxCustomers.
  68.     super initialize.!
  69.  
  70. length
  71.     ^customers size
  72.         + (nowServing isNil ifTrue:[0] ifFalse:[1]).!
  73.  
  74. nextCustomer
  75.     running
  76.         ifTrue:[
  77.             customers isEmpty
  78.                 ifTrue: [nowServing := nil]
  79.                 ifFalse:[
  80.                     nowServing := customers removeFirst.
  81.                     self send:#moveToCheckout
  82.                             to:nowServing
  83.                             with:checker.
  84.                     customers do:[:aCustomer|
  85.                         aCustomer isNil
  86.                             ifFalse:[
  87.                                 self send:#moveForward to:aCustomer]]]].!
  88.  
  89. position:aPoint
  90.     super position:aPoint.
  91.     checker position:aPoint
  92.                         + ((self width // 2)
  93.                             @ (self height // 4)).
  94.     bagger position:aPoint + (0 @ self height * 3 // 4).!
  95.  
  96. release
  97.     bagger release.  checker    release.  nowServing release.
  98.     bagger := nil.   checker := nil.      nowServing := nil.
  99.  
  100.     [customers isEmpty] whileFalse:[customers removeFirst release].
  101.     customers release.  customers  := nil.  super release.!
  102.  
  103. shutdown
  104.     |semaphore|
  105.     self update:0.
  106.     semaphore := Semaphore new.
  107.     customers do:[:aCustomer|
  108.             aCustomer stop:semaphore.
  109.             semaphore wait].
  110.     nowServing isNil
  111.         ifFalse:[
  112.             nowServing stop:semaphore.
  113.             semaphore wait].
  114.     checker isNil
  115.         ifFalse:[
  116.             checker stop:semaphore.
  117.             semaphore wait].
  118.     bagger isNil
  119.         ifFalse:[
  120.             bagger stop:semaphore.
  121.             semaphore wait].
  122.     super shutdown.!
  123.  
  124. start
  125.     super start.
  126.     bagger start.
  127.     checker start.!
  128.  
  129. update
  130.     |aValue|
  131.     ((aValue := self receive) isKindOf:Number)
  132.         ifTrue:[self update:aValue].!
  133.  
  134. update:aValue
  135.     |fieldWidth textPosition extent|
  136.     textPosition :=
  137.         position  + ((self width // 2)
  138.                         @ ((SysFont height + 2) negated)).
  139.     fieldWidth := 3.
  140.     extent := (SysFont width * fieldWidth)
  141.                 @ SysFont height.
  142.     aValue == 0
  143.         ifTrue: [Display white:(textPosition extent:extent)]
  144.         ifFalse:[(aValue printString flushedRightIn:fieldWidth)
  145.                         displayAt:textPosition].! !
  146.